const dbUtils = require('../utils/db-util');

const live = {

	/**
	 * 获取直播列表信息
	 * @param  {string} 
	 * @return {object|null}     查找结果
	 */
	async getLiveInfo(model) {
		let _sql = `
			select * from live_info where 1=1`;
		if(model.user_id) {
			_sql += ` and user_id = ${model.user_id}`;
		}
		if(model.is_live) {
			_sql += ` and is_live = ${model.is_live}`;
		}
		_sql += ` limit ${model.page_number}, ${model.page_size}`;
		let result = await dbUtils.query(_sql);
		if(Array.isArray(result) && result.length > 0) {
			result = result;
		} else {
			result = null;
		}
		return result;
	},

	/**
	 * 增加用户直播信息
	 * @param  {object} model 消息数据模型
	 * @return {object}       mysql执行结果
	 */
	async createLiveInfo(model) {
		let result = await dbUtils.insertData('live_info', model);
		return result;
	},

	/**
	 * 更新用户直播信息
	 * @param  {object} model 消息数据模型
	 * @return {object}       mysql执行结果
	 */
	async updateLiveInfo(model, userId) {
		let _sql = `
		    update live_info set ? where user_id = ${userId}`;
		let result = await dbUtils.query(_sql, model);
		return result;
	},
};

module.exports = live;